package city.spring.modules.oldman.controller;

import city.spring.modules.oldman.entity.OldmanDiningfeeEntity;
import city.spring.modules.oldman.service.OldmanDiningfeeService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：老人餐饮费
 *
 * @author 谢金婷
 * @date 2020-09-05 23:13:11
 */
@RestController
@RequestMapping("/oldman_diningfee")
public class OldmanDiningfeeController extends ApiController {
    private final OldmanDiningfeeService oldmanDiningfeeService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<OldmanDiningfeeEntity, ?>> entityOrderFields;

    public OldmanDiningfeeController(OldmanDiningfeeService oldmanDiningfeeService) {
        this.oldmanDiningfeeService = oldmanDiningfeeService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(OldmanDiningfeeEntity::getId);
        entityOrderFields.add(OldmanDiningfeeEntity::getOldmanId);
        entityOrderFields.add(OldmanDiningfeeEntity::getItemId);
        entityOrderFields.add(OldmanDiningfeeEntity::getFee);
        entityOrderFields.add(OldmanDiningfeeEntity::getReceivableThisMonth);
        entityOrderFields.add(OldmanDiningfeeEntity::getRemark);
        entityOrderFields.add(OldmanDiningfeeEntity::getGmtCreate);
        entityOrderFields.add(OldmanDiningfeeEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>老人餐饮费</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, OldmanDiningfeeEntity entity) {
        LambdaQueryChainWrapper<OldmanDiningfeeEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>老人餐饮费</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, OldmanDiningfeeEntity entity) {
        LambdaQueryChainWrapper<OldmanDiningfeeEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<OldmanDiningfeeEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>老人餐饮费</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(oldmanDiningfeeService.getById(id));
    }

    /**
     * 添加一个 <strong>老人餐饮费</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody OldmanDiningfeeEntity entity) {
        oldmanDiningfeeService.saveOldmanDiningfee(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>老人餐饮费</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody OldmanDiningfeeEntity entity) {
        entity.setId(id);
        oldmanDiningfeeService.updateOldmanDiningfee(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>老人餐饮费</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        oldmanDiningfeeService.deleteOldmanDiningfee(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>老人餐饮费</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        oldmanDiningfeeService.deleteOldmanDiningfee(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<OldmanDiningfeeEntity> buildLambdaQuery(OldmanDiningfeeEntity entity) {
        LambdaQueryChainWrapper<OldmanDiningfeeEntity> lambdaQuery = oldmanDiningfeeService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), OldmanDiningfeeEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldmanId()), OldmanDiningfeeEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getItemId()), OldmanDiningfeeEntity::getItemId, entity.getItemId());
        lambdaQuery.eq(entity.getFee() != null, OldmanDiningfeeEntity::getFee, entity.getFee());
        lambdaQuery.eq(entity.getReceivableThisMonth() != null, OldmanDiningfeeEntity::getReceivableThisMonth, entity.getReceivableThisMonth());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRemark()), OldmanDiningfeeEntity::getRemark, entity.getRemark());
        lambdaQuery.eq(entity.getGmtCreate() != null, OldmanDiningfeeEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, OldmanDiningfeeEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<OldmanDiningfeeEntity> buildQuery(OldmanDiningfeeEntity entity) {
        QueryWrapper<OldmanDiningfeeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanId()), "oldman_id", entity.getOldmanId());
        wrapper.like(StringUtils.isNotBlank(entity.getItemId()), "item_id", entity.getItemId());
        wrapper.eq(entity.getFee() != null, "fee", entity.getFee());
        wrapper.eq(entity.getReceivableThisMonth() != null, "receivable_this_month", entity.getReceivableThisMonth());
        wrapper.like(StringUtils.isNotBlank(entity.getRemark()), "remark", entity.getRemark());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

    /**
     * 根据oldmanId查找老人餐饮费
     *
     * @param oldmanId
     * @return
     */
    @GetMapping("getDiningfeeByOldmanId/{oldmanId}")
    public Object getDiningfeeByOldmanId(@PathVariable String oldmanId) {
        return success(oldmanDiningfeeService.getDiningfeeByOldmanId(oldmanId));
    }

}
